package com.peng.crm.workbench.controller;

import com.peng.crm.commons.utils.DateUtil;
import com.peng.crm.commons.utils.Result;
import com.peng.crm.commons.utils.UUIDUtil;
import com.peng.crm.settings.entity.User;
import com.peng.crm.workbench.entity.Activity;
import com.peng.crm.workbench.service.ActivityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 市场活动表(Activity)表控制层
 *
 * @author makejava
 * @since 2022-07-02 19:11:30
 */
@Controller
@RequestMapping("/activity")
public class ActivityController {
    /**
     * 服务对象
     */
    @Resource
    private ActivityService activityService;

    /**
     * 分页查询
     *
     * @param pageNumber 页码
     * @param pageSize   条数
     * @param activity   筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ResponseBody
    public Result listPage(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                           Activity activity) {
        return Result.success(activityService.listPage(pageNumber, pageSize,activity));
    }

    /**
     * 查询全部
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(activityService.list());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/find/{id}")
    @ResponseBody
    public Result  queryById(@PathVariable("id") String id) {
        return Result.success(activityService.queryById(id));
    }

    /**
     * 通过主键查询单条数据详情
     *
     * 同步处理：跳转页面，数据存放在request域对象中
     *
     * @param id 主键
     * @return 响应格式
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, HttpServletRequest request) {
        request.setAttribute("activity",activityService.queryById(id));
        return "pages/workbench/activity/detail";
    }



    /**
     * 新增数据
     *
     * @param activity 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ResponseBody
    public Result  add(@RequestBody Activity activity, HttpSession session) {
        User loginUser = (User)session.getAttribute("loginUser");
        //手动维护ID
        activity.setId(UUIDUtil.getUUID());
        //设置创建者，修改者
        activity.setCreateBy(loginUser.getLoginAct());
        activity.setEditBy(loginUser.getLoginAct());
        //设置创建时间，修改时间
        activity.setCreateTime(DateUtil.formatDate(new Date()));
        activity.setEditTime(DateUtil.formatDate(new Date()));
        return Result.success(activityService.insert(activity));
    }

    /**
     * 编辑数据
     *
     * @param activity 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @ResponseBody
    public Result  edit(@RequestBody Activity activity,HttpSession session) {
        //从session域中取到当前登陆的用户
        User loginUser = (User)session.getAttribute("loginUser");
        //设置修改者
        activity.setEditBy(loginUser.getLoginAct());
        //设置修改时间
        activity.setEditTime(DateUtil.formatDate(new Date()));
        return Result.success(activityService.update(activity));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteById")
    @ResponseBody
    public Result deleteById(String id) {
        return Result.success(activityService.deleteById(id));
    }

    /**
     * 删除多条数据
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/removes")
    @ResponseBody
    public Result removes(String[] ids) {
        return Result.success(activityService.removeBatch(ids));
    }

}

